package com.jinxin.platform.cdperprotypay.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinxin.platform.cdperprotypay.mapper.MeterDataMapper;
import com.jinxin.platform.cdperprotypay.pojo.domain.MeterData;
import com.jinxin.platform.cdperprotypay.pojo.domain.RoomUser;
import com.jinxin.platform.cdperprotypay.pojo.form.MeterDateForm;
import com.jinxin.platform.cdperprotypay.pojo.vo.MeterDataVO;
import com.jinxin.platform.cdperprotypay.service.MeterDataService;
import com.jinxin.platform.cdperprotypay.support.ModularSupport;
import org.springframework.stereotype.Service;
import com.jinxin.platform.base.common.pojo.JsonResult;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 抄表数据表(MeterData)表服务实现类
 *
 * @author zhangpeng
 * @since 2020-06-28 17:30:38
 */
@Service("meterDataService")
public class MeterDataServiceImpl implements MeterDataService {
    @Resource
    private MeterDataMapper meterDataMapper;
    @Resource
    private ModularSupport modularSupport;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JsonResult queryById(String id) {
        MeterData meterData = this.meterDataMapper.queryById(id);
        return JsonResult.ok(meterData);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param meterData 实例对象
     * @return 对象列表
     */
    @Override
    public JsonResult queryAll(MeterData meterData) {
        List<MeterData> meterDataList = this.meterDataMapper.queryAll(meterData);
        return JsonResult.ok(meterDataList);
    }

    /**
     * 新增数据
     *
     * @param meterData 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult insert(MeterData meterData) {
        int i = this.meterDataMapper.insert(meterData);
        return i > 0 ? JsonResult.ok("新增成功", meterData) : JsonResult.error("新增失败！");
    }

    /**
     * 修改数据
     *
     * @param meterData 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult update(MeterData meterData) {
        int i = this.meterDataMapper.update(meterData);
        return i > 0 ? JsonResult.ok("修改成功！", null) : JsonResult.error("修改失败！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public JsonResult deleteById(String id) {
        return this.meterDataMapper.deleteById(id) > 0 ? JsonResult.ok("删除成功！", null) : JsonResult.error("删除失败！");
    }

    @Override
    public JsonResult pageList(MeterDateForm meterDateForm) {
        Page<Object> page = PageHelper.startPage(meterDateForm.getPage(), meterDateForm.getLimit());

        List<MeterDataVO> meterData = meterDataMapper.pageList(meterDateForm);
        for (MeterDataVO c : meterData) {
            HashMap<String, String> map = new HashMap<>();
            map.put("roomId", c.getRoomId());
            List<RoomUser> userByRId = modularSupport.getUserByRId(map);
            for (RoomUser r : userByRId) {
                if (r.getUserType().equals("11")) {
                    c.setUserName(r.getUserName());
                }
            }
            if (c.getMeterItems() != null && c.getMeterItems().equals("水费")) {
                c.setItemsName("水费");
            }
        }

        PageInfo<MeterDataVO> pageInfo = new PageInfo<>(meterData);
        pageInfo.setTotal(page.getTotal());
        pageInfo.setPages(page.getPages());
        return JsonResult.ok(pageInfo);
    }
}